package me.luger.project.picc.controller.api;

import me.luger.core.commom.util.FileUtil;
import me.luger.core.commom.util.LogUtil;
import me.luger.core.commom.util.StringUtil;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.project.picc.entity.PiccMeeting;
import me.luger.project.picc.service.PiccMeetingService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ResponseBody;

import me.luger.project.picc.service.DrawListService;
import me.luger.project.picc.entity.DrawList;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * DrawList controller
 */
@Controller
@RequestMapping("/api/picc/drawList")
public class DrawListApiController extends BaseController {

    @Autowired
    private DrawListService drawListService;

    @Autowired
    private PiccMeetingService piccMeetingService;

    /**
     * update piccMeeting set doPrizeStatus = 1 ;
     * update drawList set doPrizeStatus = 1 ;
     *
     * type=1人民健康一卡通就医服务卡新客户抽奖【总参与人数23人】
     * type=2PICC人民健康APP注册抽奖【总参与人数26万+人】
     * @param rank
     * @param count
     * @param type
     * @return
     */
    @RequestMapping(value = "/doDraw",name = "领导抽奖仪式接口")
    @ResponseBody
    public Object doDraw(int rank,int count,int type){
        List<DrawList> drawListList = null;
        try{
            drawListList = drawListService.doDraw(rank,count,type);

            if(null == drawListList){
                return Result.info(-1,"没有符合条件的中奖者了");
            }
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("userList",drawListList);
    }

    @RequestMapping(value = "/export2Excel",name = "导出抽奖数据")
    @ResponseBody
    public Object export2Excel(int type){
        String filePath = "";
        if(type == 1){
            filePath = drawListService.export2Excel(1);
        }else if(type == 2){
            filePath = drawListService.export2Excel(2);
        }if(type == 3){
            filePath = piccMeetingService.export2Excel();
        }

        if(StringUtil.isBlank(filePath)){
            return Result.error("还未进行抽奖",null);
        }
        return Result.success().addDateItem("filePath",filePath);
    }

    @RequestMapping(value = "/resetDoPrizeStatus",name = "重置抽奖状态")
    @ResponseBody
    public Object resetDoPrizeStatus(){
        try{
            drawListService.resetDoPrizeStatus();
            piccMeetingService.resetDoPrizeStatus();
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "",name = "接口描述")
    @ResponseBody
    public Object demo(){
        try{

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }
}
